home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr31 / rle8_sc.zip / UNRLE8.C < prev    next >
Text File  |  1993-05-14  |  3KB  |  100 lines

  1. /* unrle8.c                            */
  2. /* RLE compression, uses 8 bit headers */
  3. /* by Shaun Case 1991 Borland C++ 2.0  */
  4. /* Public Domain                       */
  5.  
  6. #include <stdio.h>
  7. #include <string.h>
  8.  
  9. #include "rle8.h"
  10.  
  11. int main(int argc, char **argv)
  12. {
  13.     register int byte;
  14.     register unsigned short i;
  15.     register unsigned short length;
  16.     int packet_hdr;
  17.     char orig_filename[14]; /* original filename */
  18.     char *infile_name;
  19.     char scratch_space[134];
  20.  
  21.  
  22.     FILE *infile, *outfile;
  23.  
  24.     if (argc != 2)
  25.     {
  26.         puts("Usege: unrle8 filename");
  27.         return 1;
  28.     }
  29.     puts("unlre8   by Shaun Case 1991  public domain");
  30.  
  31.     infile_name = argv[1];
  32.  
  33.     if ((infile=fopen(infile_name, "rb")) == NULL)
  34.     {
  35.         strcpy(scratch_space, "Unable to open ");
  36.         strcat(scratch_space, infile_name);
  37.         puts(scratch_space);
  38.         return 1;
  39.     }
  40.  
  41.     for (i = 0; i < 13; i++)   /* get original filename */
  42.         if ((orig_filename[i] = fgetc(infile)) == EOF)
  43.         {
  44.             puts("Error reading original filename from input file.");
  45.             return 1;
  46.         }
  47.  
  48.     if ((outfile=fopen(orig_filename, "wb")) == NULL)
  49.     {
  50.         strcpy(scratch_space, "Unable to open ");
  51.         strcat(scratch_space, orig_filename);
  52.         puts(scratch_space);
  53.         return 1;
  54.     }
  55.  
  56.  
  57.     while (!feof(infile))
  58.     {
  59.         packet_hdr = fgetc(infile);
  60.  
  61.         if (feof(infile))
  62.             continue;
  63.  
  64.         length = MAX_LEN & packet_hdr;
  65.  
  66.         if (packet_hdr & RUN)  /* if it's a run... */
  67.         {
  68.             byte = fgetc(infile);
  69.  
  70.             for (i = 0; i < length; i++)
  71.                 if (fputc(byte, outfile)== EOF)
  72.                 {
  73.                     strcpy(scratch_space, "Error writing to ");
  74.                     strcat(scratch_space, orig_filename);
  75.                     puts(scratch_space);
  76.                     fclose(infile);
  77.                     fclose(outfile);
  78.                     return 1;
  79.                 }
  80.         }
  81.  
  82.         else /* it's a sequence */
  83.  
  84.             for (i = 0; i < length; i++)
  85.                 if (fputc(fgetc(infile), outfile)==EOF)
  86.                 {
  87.                     strcpy(scratch_space, "Error writing to ");
  88.                     strcat(scratch_space, orig_filename);
  89.                     puts(scratch_space);
  90.                     fclose(infile);
  91.                     fclose(outfile);
  92.                     return 1;
  93.                 }
  94.     }
  95.     fclose(infile);
  96.     fclose(outfile);
  97.     return 0;
  98. }
  99.  
  100.